/*
* PROJECT: Happy New Year
* VERSION: 1.0
* AUTHOR: Mattias Thell
* URL: http://www.mattiasthell.com
*
* */

#include "Actor.hpp"

#include "World.hpp"
#include "ActorManager.hpp"

namespace hny { 

	Actor::Actor(World* world, TypeType type)
		:mWorld(world), mPosition(hgeVector(0,0)), mVelocity(hgeVector(0,0)), 
		mCollidable(true), mRadius(0), mAlive(true), mKind(type), mIsDying(false)
	{
		mId = ActorManager::GetInstance()->GetNewId();
	}

	Actor::~Actor()
	{
	}

	void Actor::OnDeath() 
	{}

	bool Actor::CollidesWith(const Actor& actor) {
		
		// It makes no sense to collide with oneself
		if(GetId() == actor.GetId()) 
			return false;

		Circle c;
		c.Position = actor.GetPosition();
		c.Radius = actor.GetRadius();
		return CollidesWith(c);
	}
	
	bool Actor::CollidesWith(Circle c) {
		hgeVector mp = GetPosition();
		float mr = GetRadius();

		hgeVector cp = c.Position;
		float cr = c.Radius;

		hgeVector diff = mp - cp;
		float lengthSq = (cp.x - mp.x)*(cp.x - mp.x) + (cp.y - mp.y)*(cp.y - mp.y);
		float l = sqrtf(lengthSq);

		if(l > cr + mr)
			return false;

		return true;

	}

	const hgeVector& Actor::GetPosition() const					{ return mPosition; }
	void			 Actor::SetPosition(const hgeVector& pos)	{ mPosition = pos; }

	const hgeVector& Actor::GetVelocity() const					{ return mVelocity; }
	void			 Actor::SetVelocity(const hgeVector& vel)	{ mVelocity = vel; }

	bool			 Actor::IsCollidable() const				{ return mCollidable; }
	void			 Actor::SetCollidable(bool c)				{ mCollidable = c; }

	float			 Actor::GetRadius()	const					{ return mRadius; }
	void			 Actor::SetRadius(float radius)				{ mRadius = radius; }

	bool			 Actor::IsAlive() const						{ return mAlive; }
	bool			 Actor::IsDying() const						{ return mIsDying; }

	void Actor::SetIsDying() { 
		OnDeath(); mIsDying = true; 
	}
	
	void Actor::Kill() { 
		if(!mIsDying) OnDeath(); 
		mAlive = false; 
	}

	World*			 Actor::GetWorld() const					{ return mWorld; }

	IdType			 Actor::GetId() const						{ return mId; }
	TypeType		 Actor::GetKind() const						{ return mKind; }
} 